<template>
  <el-container class="layout-container">
    <!-- 侧边栏 -->
    <el-aside :width="isCollapse ? '64px' : '200px'" class="aside">
      <div class="logo" @click="handleLogoClick">
        <img src="/vite.svg" alt="Logo" :class="{ 'collapsed': isCollapse }">
        <span v-show="!isCollapse">后台管理系统</span>
      </div>
      <el-menu
        :default-active="activeMenu"
        class="el-menu-vertical"
        :collapse="isCollapse"
        @select="handleSelect"
        background-color="#304156"
        text-color="#bfcbd9"
        active-text-color="#409EFF"
      >
        <el-menu-item index="/blog">
          <el-icon><Grid /></el-icon>
          <template #title>博客管理</template>
        </el-menu-item>
        <el-menu-item index="/role">
          <el-icon><UserFilled /></el-icon>
          <template #title>角色管理</template>
        </el-menu-item>
        <el-menu-item index="/child">
          <el-icon><Monitor /></el-icon>
          <template #title>Vue应用</template>
        </el-menu-item>
        <el-menu-item index="/react-app">
          <el-icon><Monitor /></el-icon>
          <template #title>React应用</template>
        </el-menu-item>
      </el-menu>
    </el-aside>

    <el-container class="main-container">
      <!-- 顶部栏 -->
      <el-header class="header">
        <div class="header-left">
          <el-icon 
            class="collapse-btn"
            @click="toggleCollapse"
          >
            <Fold v-if="!isCollapse"/>
            <Expand v-else/>
          </el-icon>
          <el-breadcrumb separator="/">
            <el-breadcrumb-item @click="handleBreadcrumbClick('/')">首页</el-breadcrumb-item>
            <el-breadcrumb-item v-if="currentPath" @click="handleBreadcrumbClick(currentRoute)">
              {{ currentPath }}
            </el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <div class="header-right">
          <el-dropdown>
            <span class="user-info">
              <el-avatar :size="32" src="/vite.svg"/>
              <span>管理员</span>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item>个人信息</el-dropdown-item>
                <el-dropdown-item>退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <!-- 内容区 -->
      <el-main class="main">
        <router-view v-slot="{ Component }">
          <component :is="Component" />
        </router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup lang="ts">
import { ref, computed, onBeforeUnmount } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import microApp from '@micro-zoe/micro-app'

const route = useRoute()
const router = useRouter()
const isCollapse = ref(false)
const activeMenu = computed(() => route.path)

// 当前路由路径
const currentRoute = computed(() => {
  switch (route.path) {
    case '/child':
      return '/child'
    case '/role':
      return '/role'
    case '/react-app':
      return '/react-app'
    default:
      return '/'
  }
})

const currentPath = computed(() => {
  switch (route.path) {
    case '/':
      return '首页'
    case '/blog':
      return '博客管理'
    case '/role':
      return '角色管理'
    case '/child':
      return 'Vue应用'
    case '/react-app':
      return 'React应用'
    default:
      return ''
  }
})

const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}

const handleSelect = (key: string) => {
  router.push(key)
}

const handleLogoClick = () => {
  router.push('/')
}

const handleBreadcrumbClick = (path: string) => {
  router.push(path)
}

// 组件卸载前清理所有子应用
onBeforeUnmount(() => {
  microApp.unmountAllApps()
})
</script>

<style scoped>
/* 移除全局样式重置（已经在 global.css 中定义） */
.layout-container {
  height: 100vh;
  width: 100vw;
  margin: 0;
  padding: 0;
  display: flex;
}

.aside {
  background-color: #304156;
  transition: width 0.3s;
  overflow: hidden;
  height: 100vh;
  flex-shrink: 0;
  padding: 0;
}

.logo {
  height: 60px;
  display: flex;
  align-items: center;
  padding: 0 20px;
  color: #fff;
  background-color: #2b2f3a;
  font-size: 16px;
  white-space: nowrap;
  overflow: hidden;
  cursor: pointer;
  transition: background-color 0.3s;
}

.logo:hover {
  background-color: #263445;
}

.logo img {
  width: 32px;
  height: 32px;
  margin-right: 12px;
  transition: all 0.3s;
}

.logo img.collapsed {
  margin-right: 0;
}

.el-menu-vertical {
  border-right: none;
  height: calc(100vh - 60px);
}

.el-menu-vertical:not(.el-menu--collapse) {
  width: 200px;
}

.main-container {
  flex: 1;
  height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 0;
  margin: 0;
}

.header {
  background-color: white;
  border-bottom: 1px solid #e6e6e6;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  height: 60px;
  width: 100%;
  flex-shrink: 0;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 20px;
}

.collapse-btn {
  font-size: 20px;
  cursor: pointer;
  color: #303133;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  color: #303133;
}

.main {
  background-color: #f0f2f5;
  padding: 0;
  flex: 1;
  overflow: hidden;
  width: 100%;
  margin: 0;
  display: flex;
}

:deep(.el-breadcrumb__inner) {
  color: #606266;
  cursor: pointer;
  transition: color 0.3s;
}

:deep(.el-breadcrumb__inner:hover) {
  color: #409EFF;
}

:deep(.el-breadcrumb__item:last-child .el-breadcrumb__inner) {
  color: #303133;
  cursor: pointer;
}

:deep(.el-breadcrumb__item:last-child .el-breadcrumb__inner:hover) {
  color: #409EFF;
}

/* 覆盖 Element Plus 的默认样式 */
:deep(.el-container) {
  margin: 0;
  padding: 0;
  width: 100%;
}

:deep(.el-aside) {
  padding: 0;
  margin: 0;
}

:deep(.el-main) {
  padding: 0;
  margin: 0;
  flex: 1;
  display: flex;
  flex-direction: column;
}

/* 添加新的样式来控制内容的间距 */
:deep(.el-row) {
  margin: 0 !important;
}

:deep(.el-col) {
  padding: 0 !important;
}
</style>